import { getToken } from "@/utils/auth";
import request from "../../../../utils/request";

export async function rule(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
}, options?: { [key: string]: any }) {
  const res = await request('/menu', {
    params,
    method: 'GET',
    ...(options || {}),
  });
  //console.log('token:',getToken())
  console.log('请求菜单', res)
  return {
    data: res.data.records,
    page: params.current,
    success: true,
    total: res.data.total,
  }
}

export async function updateRule(data: { [key: string]: any }, options?: { [key: string]: any }) {
  return request('/menu', {
    data: {
      ...data,
      updataTime: new Date().getTime()
    },
    method: 'PUT',
    ...(options || {}),
  });
}

export async function addRule(data: { [key: string]: any }, options?: { [key: string]: any }) {
  return request('/menu', {
    data: {
      ...data,
      createTime: new Date().getTime()
    },
    method: 'POST',
    ...(options || {}),
  });
}

export async function removeRule(data: { key: number[] }, options?: { [key: string]: any }) {
  return request('/menu', {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}


export async function allMenuWithRole(params: {
  roleId?: number
}, options?: { [key: string]: any }) {
  if (!params.roleId) {
    return null
  }
  return request(`/menu/role/${params.roleId}`, {
    method: 'GET',
    ...(options || {}),
  });
}


export async function updateMenuRole(params: {
  roleId?: number,
  ids?: number[]
}, options?: { [key: string]: any }) {
  const data = {
    ids: params.ids
  }
  return request(`/menu/role/${params.roleId}`, {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}